package com.example.app_mqttclient.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.app_mqttclient.fragment.DeviceChooseFragment;
import com.example.app_mqttclient.fragment.DeviceSettingFragment;
import com.example.app_mqttclient.R;

public class DeviceChooseActivity extends FragmentActivity {

    private static final String TAG = "DeviceChooseActivity";

    private int deviceImgID = 0;
    private String deviceName = "";
    private String devicePublishTopic = "";
    private String deviceSubscribeTopic = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_choose);


        replaceFragment(new DeviceChooseFragment()); // 切换Fragment
    }

    @Override
    public void onBackPressed() {
        deviceChooseBackBtnOnClick(null);
    }

    /**
     * @brief   设备选择页面的back键点击处理
     * @param   view: view对象
     * @retval  None
     */
    public void deviceChooseBackBtnOnClick(View view)
    {
        Log.i(TAG, "device choose back btn onClick");
        this.deviceName = "";
        this.devicePublishTopic = "";
        this.deviceSubscribeTopic = "";
        this.deviceImgID = 0;
        // 使用Intent传递数据
        Intent intent = new Intent();
        // 通过key-value方式传递
        intent.putExtra("deviceName", this.deviceName);
        intent.putExtra("publishTopic", this.devicePublishTopic);
        intent.putExtra("subscribeTopic", this.deviceSubscribeTopic);
        intent.putExtra("deviceImgID", this.deviceImgID);
        setResult(3, intent); // 返回数据给上个Activity  与onActivityResult配合使用
        this.finish(); // 结束当前页面
    }
    /**
     * @brief   设备选择页面的按键点击处理
     * @param   view: view对象
     * @retval  None
     */
    public void deviceChooseActivityOnClick(View view)
    {
        switch (view.getId())
        {
            case R.id.device_choose_btn_normal_light:
                this.deviceImgID = R.drawable.lightbulb;

                Log.i(TAG, "device choose normal light btn onClick");
                break;
            case R.id.device_choose_btn_colour_light:
                this.deviceImgID = R.drawable.lightoutline;

                Log.i(TAG, "device choose colour light btn onClick");
                break;
            case R.id.device_choose_btn_audio:
                this.deviceImgID = R.drawable.ic_baseline_music_video_24;

                Log.i(TAG, "device choose audio btn onClick");
                break;
            case R.id.device_choose_btn_refrigerator:
                this.deviceImgID = R.drawable.refrigerator;

                Log.i(TAG, "device choose refrigerator btn onClick");
                break;
            case R.id.device_choose_btn_tv:
                this.deviceImgID = R.drawable.tv;

                Log.i(TAG, "device choose tv btn onClick");
                break;
            case R.id.device_choose_btn_sensor:
                this.deviceImgID = R.drawable.sensor;

                Log.i(TAG, "device choose sensor btn onClick");
                break;
            case R.id.device_choose_btn_socket:
                this.deviceImgID = R.drawable.socket;

                Log.i(TAG, "device choose socket btn onClick");
                break;
            case R.id.device_choose_btn_block:
                this.deviceImgID = R.drawable.block;

                Log.i(TAG, "device choose block btn onClick");
                break;
        }
        // 切换Fragment页面
        replaceFragment(new DeviceSettingFragment(view.getId()));
    }
    /**
     * @brief   设备设置页面的确认键点击处理
     * @param   view: view对象
     * @retval  None
     */
    public void deviceSureBtnOnClick(View view)
    {
        EditText devNameEditText = findViewById(R.id.device_setting_device_name);
        this.deviceName = devNameEditText.getText().toString();
        EditText publishTopicEditText = findViewById(R.id.device_setting_publish_topic);
        this.devicePublishTopic = publishTopicEditText.getText().toString();
        EditText subscribeTopicEditText = findViewById(R.id.device_setting_subscribe_topic);
        this.deviceSubscribeTopic = subscribeTopicEditText.getText().toString();
        // 设备名称和publish topic和subscribe topic不为空
        if(!this.deviceName.isEmpty() && !this.devicePublishTopic.isEmpty() && !this.deviceSubscribeTopic.isEmpty())
        {
            Log.i(TAG, "deviceName: " + this.deviceName + " ,imgID: " + this.deviceImgID + " ,pubTopic: " + this.devicePublishTopic + " ,subTopic: " + this.deviceSubscribeTopic);
            // 使用Intent传递数据
            Intent intent = new Intent();
            // 通过key-value方式传递
            intent.putExtra("deviceName", this.deviceName);
            intent.putExtra("publishTopic", this.devicePublishTopic);
            intent.putExtra("subscribeTopic", this.deviceSubscribeTopic);
            intent.putExtra("deviceImgID", this.deviceImgID);
            setResult(3, intent); // 返回数据给上个Activity  与onActivityResult配合使用
            this.finish(); // 结束当前页面
        } // 若为空不退出当前界面并提醒用户
        else
        {
            Log.e(TAG, "device name or device publish topic or device subscribe topic is null!");
            // 显示Toast消息提示框
            Toast.makeText(DeviceChooseActivity.this, "设备名称, publish topic, subscribe topic不能为空!", Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * @brief   设备设置页面的确认键点击处理
     * @param   fragment: fragment对象
     * @retval  None
     */
    private void replaceFragment(Fragment fragment)
    {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.device_choose_frameLayout, fragment);
        fragmentTransaction.commit();
    }

}
